import scrapy
from MySpider.mydb import MyDB
from MySpider.mymethod import get_cookie


class MySpider(scrapy.Spider):
    def __int__(self, site_name):
        super().__init__()
        name = site_name + "DetailSpider"
        mydb = MyDB()
        spider_parameters = mydb.get_spider_db_parameters(name)
        self.allowed_domains = [spider_parameters.get('allowed_domain')]

        # 数据库相关
        unique_key = spider_parameters.get('unique_key')
        table_fields = ['url_id', 'img', 'title', 'intro', 'baidu_url', 'game_pass']
        values_params = '%s, ' * (len(table_fields) - 1) + '%s'
        keys = ', '.join(table_fields)
        index_table_name = site_name + "Index"
        detail_table_name = site_name + "Detail"
        insert_sql = 'insert into %s (%s) values (%s)' % (detail_table_name, keys, values_params)

        crawl_count = 300
        select_sql = 'select url_id from %s where url_id not in (select url_id from %s) LIMIT %s' \
                     % (index_table_name, detail_table_name, crawl_count)

        self.start_urls = ['https://www.688qf.com/%s' % d for d in mydb.get_url_ids(select_sql)]

        # cookie信息

        cookies = get_cookie(spider_parameters.get('cookie'))
